<?php

class AjaxController extends Zend_Controller_Action
{

    public function init()
    {
       // no Layout
			$this->_helper->layout()->disableLayout();
			// no views
    }

    public function indexAction()
    {
        // action body
    }
    
    public function allcategoryAction(){
    	$this->views->tt="Loan ngoc";
    	
    }
    
    public function searchAction()
    {
    	$q=$this->getRequest()->getParam('q');
    	$type=$this->getRequest()->getParam('type');
    	if ($type=="dish"){
    		
    		$dishMapper=new Model_DishMapper();
    		$data=$dishMapper->findDishes($q);
    		$this->view->dishes=$data;
    		$this->view->type=$type;
    	}else if ($type=="location"){
    		$locM=new Model_LocationMapper();
    		$data=$locM->findLocations($q);
    		$this->view->locations=$data;
    		$this->view->type=$type;
    	}
    	else {
    		$dishMapper=new Model_DishMapper();
    		$data1=$dishMapper->findDishes($q);
    		$this->view->dishes=$data1;
    		$locM=new Model_LocationMapper();
    		$data2=$locM->findLocations($q);
    		$this->view->locations=$data2;
    		$this->view->type=$type;
    		
    	}
    	
    }
    
    public function topcategoryAction(){
    	$this->views->tt="Loan ngoc";
    }

    public function alldistrictAction(){
    	
    }
    public function topdistrictAction(){
    	 
    }
    
    public function removeitemincartAction(){
    	$dishLocId=$this->getRequest()->getParam('dishLocId');
    	$locId=$this->getRequest()->getParam('locId');
    	$cart=new Model_CartHelper();
    	$rr=$cart->removeDish($dishLocId, $locId);
    	if ($rr<=0){
    		$cart->removeLoc($locId);
    	}
    }
    
    public function edititemincartAction(){
    	$dishLocId=$this->getRequest()->getParam('dishLocId');
    	$locId=$this->getRequest()->getParam('locId');
    	$quantity=$this->getRequest()->getParam('quantity');
    	$cart=new Model_CartHelper();
    	$rr=$cart->editDish($dishLocId, $locId, $quantity);
    	
    }
    
    
    public function removelocincartAction(){
    	$locId=$this->getRequest()->getParam('locId');
    	$cart=new Model_CartHelper();
    	$cart->removeLoc($locId);
    }
    
    
    public function updatecartAction(){
    	$id=$this->getRequest()->getParam('id');
    	$quantity=$this->getRequest()->getParam('quantity');
    	$cart=new Model_CartHelper();
    	//lay tu du lieu
    	$mapper=new Model_DishLocationMapper();
    	$item=$mapper->getDishLocation($id);
    	if ($item!=null){
    		$cart->addNewItem($id,$item['Name'], $quantity, $item['Price']);
    	}
    	
    }

    public function carteditAction(){
    	$id=$this->getRequest()->getParam('id');
    	$cart=new Model_CartHelper();
    	//edit function here
    }
    public function lastestcartAction(){
    	
    }
    
    public function getdetaildeliverystringAction(){
    	$locid=$this->getRequest()->getParam('loc_id');
    	$disid=$this->getRequest()->getParam('dis_id');
    	$id=$this->getRequest()->getParam('itemid');
    	$deliveryMapper=new Model_DeliveryMapper();
    	$dis=$deliveryMapper->getDeliveryOfLocation($locid);
    
    	
    	for($i=0;$i<count($dis);$i++){
    		if ($dis[$i]->getDistrict()->getId()==$disid){
    			//return
    			//$this->view->locid=$locid;
    			//$this->view->disid=$disid;
    			$cart=new Model_CartHelper();
    			$cart->updateDelivery($locid, $dis[$i]->getId());
    			
    			$this->view->data='<span>Time: '.$dis[$i]->getTime().'</span>'.'<span>. Fee: '.$dis[$i]->getFee().'</span>';
    			$this->view->hidden='<input type="hidden" id="fee'.$id.'" value="'.$dis[$i]->getFee().'" />';
    			return;
    		}
    		else{
    			$this->view->data="";
    		}
    	}
    }
    
    public function totalmoneyAction(){
    	$cart=new Model_CartHelper();
    	$this->view->data=$cart->calculateMoney();
    	
    }
    
}

